<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="主键id" prop="id" v-if="dataForm.id">
        <el-input
          v-model="dataForm.id"
          placeholder="主键id"
          disabled
        ></el-input>
      </el-form-item>
      <el-form-item label="看板名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="看板名称"></el-input>
      </el-form-item>
      <el-form-item label="封面url(图片url)" prop="coverUrl">
        <el-input
          v-model="dataForm.coverUrl"
          placeholder="封面url(图片url)"
        ></el-input>
      </el-form-item>
            <el-form-item label="应用url" prop="coverUrl">
        <el-input
          v-model="dataForm.baseUrl"
          placeholder="应用url"
        ></el-input>
      </el-form-item>
      <el-form-item label="介绍" prop="introduce">
        <el-input v-model="dataForm.introduce" placeholder="介绍"></el-input>
      </el-form-item>
      <el-form-item label="是否发布" prop="status">
        <el-switch
          v-model="dataForm.status"
          active-text="已发布"
          inactive-text="未发布"
          active-value="1"
          inactive-value="0"
        >
        </el-switch>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime" v-if="dataForm.id">
        <el-input
          v-model="dataForm.createTime"
          placeholder="创建时间"
          disabled
        ></el-input>
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime" v-if="dataForm.id">
        <el-input
          v-model="dataForm.updateTime"
          placeholder="更新时间"
          disabled
        ></el-input>
      </el-form-item>
      <el-form-item label="创建人" prop="createBy" v-if="dataForm.id">
        <el-input
          v-model="dataForm.createBy"
          placeholder="创建人"
          disabled
        ></el-input>
      </el-form-item>
      <el-form-item label="更新人" prop="updateBy" v-if="dataForm.id">
        <el-input
          v-model="dataForm.updateBy"
          placeholder="更新人"
          disabled
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit"
        >确定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj } from "@/api/srsbbulletinboard";

export default {
  data() {
    return {
      visible: false,
      canSubmit: false,
      dataForm: {
        id: 0,
        name: "",
        coverUrl: "",
        introduce: "",
        baseUrl:"",
        status: "",
        createTime: "",
        updateTime: "",
        createBy: "",
        updateBy: "",
      },
      dataRule: {
        name: [
          { required: true, message: "看板名称不能为空", trigger: "blur" },
        ],
        coverUrl: [
          {
            required: true,
            message: "封面url(图片url)不能为空",
            trigger: "blur",
          },
        ],
        introduce: [
          { required: true, message: "介绍不能为空", trigger: "blur" },
        ],
        status: [
          {
            required: true,
            message: "发布状态 0-未发布 1-发布不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (id) {
          getObj(id).then((response) => {
            this.dataForm = response.data.data;
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm)
              .then((data) => {
                this.$notify.success("修改成功");
                this.visible = false;
                this.$emit("refreshDataList");
              })
              .catch(() => {
                this.canSubmit = true;
              });
          } else {
            addObj(this.dataForm)
              .then((data) => {
                this.$notify.success("添加成功");
                this.visible = false;
                this.$emit("refreshDataList");
              })
              .catch(() => {
                this.canSubmit = true;
              });
          }
        }
      });
    },
    //重置表单
    closeDialog() {
      this.$refs["dataForm"].resetFields();
    },
  },
};
</script>
